/*
 * PageList.java 1.0 Created on 2003. 2. 24. 
 */
 
package com.zll.framework.grid;

import java.text.*;
import java.util.*;
import java.io.*;
import java.util.regex.*;

import com.zll.framework.model.HashList;
import com.zll.framework.util.StringUtil;


public class Grid {
	
	
	private String gridName;
	private int rowHeight;
	private String calendarImgUrl = "/img/icon/ico_calendar.gif";
	private String popupImgUrl 		= "/img/icon/ico_popup.gif";
	private String defaultColumnStyle = " class=\"tb_list\" ";
	
	private HashList dataList;
	private ArrayList columnList;
	
	
	
	
	public Grid(String gridName, HashList dataList) 
	{
		this.gridName = gridName;
		this.dataList = dataList;
		this.columnList = new ArrayList();
	}
	
	public void setRowHeight(int rowHeight) {
		this.rowHeight = rowHeight;
	}
	
	
	public GridColumn addColumn(String columnStyle)
	{
		if (columnStyle.indexOf("class") < 0) {
			columnStyle += defaultColumnStyle;
		}
		GridColumn col = new GridColumn(columnStyle);	
		columnList.add(col);
		return col;
	}
	
	public GridColumn addColumn()
	{
		return addColumn(defaultColumnStyle);
	}
	
	
	public void create(Writer out) throws Exception
	{
		StringBuffer sb = new StringBuffer();
		
		//데이타셋 정보로 첫행을 만들고 숨겨놓다. 
		sb.append("<tr style=\"display:none\" height=\""+this.rowHeight+"\"> \n");
		for (int i = 0; i < columnList.size(); i++) 
		{
			GridColumn column = (GridColumn)columnList.get(i);
			ArrayList objectList = column.getObjectList();
			
			sb.append("<td " + defaultColumnStyle + column.getColumnStyle() + "> \n");
			
			for (int j = 0; j < objectList.size(); j++) 
			{
				HashMap map = (HashMap)objectList.get(j);
					
				String objectType = StringUtil.nvl(map.get("objectType"));
				String objectName = StringUtil.nvl(map.get("objectName"));
				String objectStyle = StringUtil.nvl(map.get("objectStyle"));
				
								
				String maxLength = "";
				String numberOnly = "";
				String necessary = "";
										
				if (dataList.existsColumn(objectName)) 
				{
					String columnType	= dataList.getColumnType(objectName);
											
					if ("STRING".equals(columnType)) {
						maxLength = " maxlength=\"" + dataList.getPrecision(objectName) + "\" ";
						if (objectStyle.equals("")) {
							objectStyle = " style=\"width:100%\" class=\"inputTL\" ";
						}
					} else if ("INTEGER".equals(columnType)) {
						numberOnly = " style=\"ime-mode:disabled;\" onKeyPress=\"return cfNumber(event, false)\" ";
						if (objectStyle.equals("")) {
							objectStyle = " style=\"width:100%\" class=\"inputTR\" ";
						}
					} else if ("DECIMAL".equals(columnType)) {
						numberOnly = " style=\"ime-mode:disabled;\" onKeyPress=\"return cfNumber(event, true)\" ";
						if (objectStyle.equals("")) {
							objectStyle = " style=\"width:100%\" class=\"inputTR\" ";
						}
					} else {
						if (objectStyle.equals("")) {
							objectStyle = " style=\"width:100%\" class=\"inputTL\" ";
						}
					}
					
					if (dataList.getNecessary(objectName)) {
						necessary = " necessary=\"true\" ";
					}
//System.out.println("PPPPPPPPPPPP name:" + objectName + "  type:" + columnType + "   maxlength:"+maxLength + "  number:" + numberOnly);							
				}
				
				//예약어 칼럼 : job_type, chk
				if ("job_type".equals(objectName)) {
					sb.append("<input type=\""+objectType+"\" name=\""+gridName+".job_type\" value=\"X\"  />");
				} 
				else if ("chk".equals(objectName)) {
					sb.append("<input type=\"checkbox\" name=\""+gridName+"."+objectName+"\" " + objectStyle + " /> \n");
				}
				
				//사용자 칼럼.
				else if ("text".equals(objectType)) {
					sb.append("<input type=\"text\" name=\""+gridName+"."+objectName+"\" ");
					sb.append(objectStyle).append(maxLength).append(numberOnly).append(necessary);
					sb.append(" onchange=\"fnOnChangeHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\" ");
					sb.append("/> \n");
				}
				else if ("hidden".equals(objectType)) {
					sb.append("<input type=\"hidden\" name=\""+gridName+"."+objectName+"\" ");
					sb.append(objectStyle);
					sb.append("/> \n");
				}
				else if ("checkbox".equals(objectType)) {
					sb.append("<input type=\"hidden\" name=\""+gridName+"."+objectName+"\" value=\"N\" />");
					sb.append("<input type=\"checkbox\" name=\""+gridName+"."+objectName+"_chk\" ");
					sb.append(objectStyle);
					sb.append("/> \n");
				}
				else if ("select".equals(objectType)) {
					HashList codeList = (HashList)map.get("codeList");
					sb.append("<select name=\""+gridName+"."+objectName+"\" ");
					sb.append(" onchange=\"fnOnChangeHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\" ");
					sb.append(objectStyle).append(necessary).append("> \n");
					for (int k = 0; k < codeList.size(); k++) {
						sb.append("<option value=\""+codeList.get("code",k)+"\">"+codeList.get("code_name",k)+"</option> \n");
					}
					sb.append("</select> \n");
				}
				else if ("html".equals(objectType)) {
					sb.append(objectName);
				}
				else if ("calendar".equals(objectType)) {
					sb.append("<nobr>");
					sb.append("<input type=\"text\" name=\""+gridName+"."+objectName+"\" maxlength=\"10\" ");
					sb.append(objectStyle).append(necessary);
					sb.append(" style=\"ime-mode:disabled;\" onKeyPress=\"return cfNumber(event, false)\" ");
					sb.append(" onchange=\"fnOnChangeHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\" ");
					sb.append(" />");
          			sb.append("<img src=\""+calendarImgUrl+"\" style=\"cursor:hand\" align=\"absmiddle\" ");
          			sb.append(" onclick=\"cfOpenCalendar(this, '"+gridName+"."+objectName+"')\">");
          			sb.append("</nobr> \n");
				}
				else if ("popup".equals(objectType)) {
					sb.append("<nobr>");
					sb.append("<input type=\"text\" name=\""+gridName+"."+objectName+"\" ");
					sb.append(objectStyle).append(maxLength).append(necessary);
					sb.append(" onchange=\"fnOnChangeHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\" ");
					sb.append(" >");
					//sb.append("<img src=\""+popupImgUrl+"\" style=\"cursor:hand\" align=\"absmiddle\" ");
          			//sb.append(" onclick=\"fnOnPopupHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\">");
          			sb.append("<a href=\"#none\" onclick=\"fnOnPopupHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\" >");
          			sb.append("<img src=\""+popupImgUrl+"\"  align=\"absmiddle\"/></a>");
          			sb.append("</nobr> \n");
				}
			}
				
			sb.append("</td> \n");
		}
		sb.append("</tr> \n");
		
		
		//실제 데이타가 있는 경우.
		for (int idx = 0; idx < dataList.size(); idx++) 
		{
			sb.append("<tr height=\""+this.rowHeight+"\"> \n");
			for (int i = 0; i < columnList.size(); i++) 
			{
				GridColumn column = (GridColumn)columnList.get(i);
				ArrayList objectList = column.getObjectList();
				
				sb.append("<td "+column.getColumnStyle()+"> \n");
				
				for (int j = 0; j < objectList.size(); j++) 
				{
					HashMap map = (HashMap)objectList.get(j);
					
					String objectType = StringUtil.nvl(map.get("objectType"));
					String objectName = StringUtil.nvl(map.get("objectName"));
					String objectStyle = StringUtil.nvl(map.get("objectStyle"));
					
					String maxLength = "";
					String numberOnly = "";
					String necessary = "";
										
					//디폴트 스타일 적용	
					if (dataList.existsColumn(objectName)) 
					{
						String columnType	= dataList.getColumnType(objectName);
						
						if ("STRING".equals(columnType)) {
							maxLength = " maxlength=\"" + dataList.getPrecision(objectName) + "\" ";
							if (objectStyle.equals("")) {
								objectStyle = " style=\"width:100%\" class=\"inputTL\" ";
							}
						} else if ("INTEGER".equals(columnType)) {
							numberOnly = " style=\"ime-mode:disabled;\" onKeyPress=\"return cfNumber(event, false)\" ";
							if (objectStyle.equals("")) {
								objectStyle = " style=\"width:100%\" class=\"inputTR\" ";
							}
						} else if ("DECIMAL".equals(columnType)) {
							numberOnly = " style=\"ime-mode:disabled;\" onKeyPress=\"return cfNumber(event, true)\" ";
							if (objectStyle.equals("")) {
								objectStyle = " style=\"width:100%\" class=\"inputTR\" ";
							}
						} else {
							if (objectStyle.equals("")) {
								objectStyle = " style=\"width:100%\" class=\"inputTL\" ";
							}
						}
						
					
						if (dataList.getNecessary(objectName)) {
							necessary = " necessary=\"true\" ";
						} 
//System.out.println("PPPPPPPPPPPP name:" + objectName + "  type:" + columnType + "   maxlength:"+maxLength 
//+ "  number:" + numberOnly + "  necessary:" + dataList.getNecessary(objectName));							
					}
					
					
					//예약어 칼럼
					if ("job_type".equals(objectName)) {
						sb.append("<input type=\""+objectType+"\" name=\""+gridName+".job_type\" value=\"X\"  />");
					}
					else if ("chk".equals(objectName)) {
						sb.append("<input type=\"checkbox\" name=\""+gridName+"."+objectName+"\" " + objectStyle + " /> \n");
					}
				
					//사용자 칼럼.
					else if ("text".equals(objectType)) {
						//sb.append("<span style=\"width:100%\" ><span>");
						sb.append("<input type=\"text\" name=\""+gridName+"."+objectName+"\" ");
						sb.append(" value=\""+dataList.get(objectName,idx)+"\" defaultValue=\""+dataList.get(objectName,idx)+"\" ");
						sb.append(objectStyle).append(maxLength).append(numberOnly).append(necessary);
						sb.append(" onchange=\"cfChangeRowStatus(this);fnOnChangeHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\" />");
						//sb.append(" onpropertychange=\"cfChangeRowStatus(this)\" />");
						//sb.append("</span></span>");
					}
					else if ("hidden".equals(objectType)) {
						sb.append("<input type=\"hidden\" name=\""+gridName+"."+objectName+"\" ");
						sb.append(" value=\""+dataList.get(objectName,idx)+"\" defaultValue=\""+dataList.get(objectName,idx)+"\" ");
						sb.append(objectStyle);
						sb.append(" />");
					}
					else if ("checkbox".equals(objectType)) {
						sb.append("<input type=\"hidden\" name=\""+gridName+"."+objectName+"\" value=\""+dataList.get(objectName,idx)+"\" />");
						sb.append("<input type=\"checkbox\" name=\""+gridName+"."+objectName+"_chk\" ");
						sb.append(objectStyle);
						if ("Y".equals(dataList.get(objectName,idx))) {
							sb.append(" checked ");
						}
						sb.append(" onclick=\"cfClickCheckBox(this)\" ");
						sb.append(" onchange=\"cfChangeRowStatus(this);fnOnChangeHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\" />");
						//sb.append(" onpropertychange=\"cfChangeRowStatus(this)\" />");
					}
					else if ("select".equals(objectType)) {
						HashList codeList = (HashList)map.get("codeList");
						sb.append("<select name=\""+gridName+"."+objectName+"\" ");
						sb.append(objectStyle).append(necessary);
						sb.append(" onchange=\"cfChangeRowStatus(this);fnOnChangeHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\" />");
						//sb.append(" onpropertychange=\"cfChangeRowStatus(this)\" />");
						for (int k = 0; k < codeList.size(); k++) {
							sb.append("<option value=\""+codeList.get("code",k)+"\" " + 
									StringUtil.selected(codeList.get("code",k),dataList.get(objectName,idx)) + " >"+codeList.get("code_name",k)+"</option> \n");
						}
						sb.append("</select> \n");
					}
					else if ("html".equals(objectType)) {
						sb.append(objectName);
					}
					else if ("value".equals(objectType)) {
						sb.append(dataList.get(objectName,idx));
					}
					else if ("calendar".equals(objectType)) {	
						sb.append("<nobr>");
						sb.append("<input type=\"text\" name=\""+gridName+"."+objectName+"\" maxlength=\"10\" ");
						sb.append(" value=\""+StringUtil.toDate(dataList.get(objectName,idx))+"\" ");
						sb.append(" defaultValue=\""+StringUtil.toDate(dataList.get(objectName,idx))+"\" ");
						sb.append(objectStyle).append(necessary);
						sb.append(" style=\"ime-mode:disabled;\" onKeyPress=\"return cfNumber(event, false)\" ");
						sb.append(" onchange=\"cfChangeRowStatus(this);fnOnChangeHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\" />");
						//sb.append(" onpropertychange=\"cfChangeRowStatus(this)\" />");
						
	          			sb.append("<img src=\""+calendarImgUrl+"\" style=\"cursor:hand\" align=\"absmiddle\" ");
	          			sb.append(" onclick=\"cfOpenCalendar(this, '"+gridName+"."+objectName+"')\">");
	          			sb.append("</nobr> \n");
					}
					else if ("popup".equals(objectType)) {
						sb.append("<nobr>");
						sb.append("<input type=\"text\" name=\""+gridName+"."+objectName+"\" ");
						sb.append(" value=\""+dataList.get(objectName,idx)+"\" ");
						sb.append(" defaultValue=\""+dataList.get(objectName,idx)+"\" ");
						sb.append(objectStyle).append(maxLength).append(necessary);
						sb.append(" onchange=\"cfChangeRowStatus(this);fnOnChangeHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\" />");
						//sb.append(" onpropertychange=\"cfChangeRowStatus(this)\" >");
						//sb.append("<img src=\""+popupImgUrl+"\" style=\"cursor:hand\" align=\"absmiddle\" ");
	          			//sb.append(" onclick=\"fnOnPopupHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\">");
	          			sb.append("<a href=\"#none\" onclick=\"fnOnPopupHandler(cfRowIndex(this), '"+gridName+"."+objectName+"')\" >");
          				sb.append("<img src=\""+popupImgUrl+"\"  align=\"absmiddle\"/></a>");
	          			sb.append("</nobr> \n");
					}
				}
					
				sb.append("</td> \n");
			}
			sb.append("</tr> \n");
		}

		
		out.write(sb.toString());
	}
	
	
	
}
